package com.worldtolearn.user.bean;

import java.util.Calendar;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;

import org.springframework.stereotype.Controller;

@Controller
@ManagedBean
@SessionScoped
public class ShowInfoMB {

	@Inject
	private LoginMB loginMB;

	public LoginMB getLoginMB() {
		return loginMB;
	}
	public void setLoginMB(LoginMB loginMB) {
		this.loginMB = loginMB;
	}


	private int clientDateOfBirthDay	= 0;
	private int clientDateOfBirthMonth	= 0;
	private int clientDateOfBirthYear	= 0;
	
	public int getClientDateOfBirthDay() {
		return clientDateOfBirthDay;
	}
	public int getClientDateOfBirthMonth() {
		return clientDateOfBirthMonth;
	}
	public int getClientDateOfBirthYear() {
		return clientDateOfBirthYear;
	}
	public void setClientDateOfBirthDay(int clientDateOfBirthDay) {
		this.clientDateOfBirthDay = clientDateOfBirthDay;
	}
	public void setClientDateOfBirthMonth(int clientDateOfBirthMonth) {
		this.clientDateOfBirthMonth = clientDateOfBirthMonth;
	}
	public void setClientDateOfBirthYear(int clientDateOfBirthYear) {
		this.clientDateOfBirthYear = clientDateOfBirthYear;
	}
	
	
	public String updateUser() {
		
		// *** Créer un rôle en dur ***
		loginMB.getUser().setRole("Client");

		// *** Créer une date conforme ***
		Calendar calendarClientBirthDate = Calendar.getInstance();
		calendarClientBirthDate.set(Calendar.MONTH, (clientDateOfBirthMonth - 1));
		calendarClientBirthDate.set(Calendar.YEAR, clientDateOfBirthYear);
		calendarClientBirthDate.set(Calendar.DAY_OF_MONTH, clientDateOfBirthDay);
		Date clientBirthDate = calendarClientBirthDate.getTime();
		loginMB.getUser().setDateOfBirth(clientBirthDate);
		
		// TODO *** A verifier que la date donnée corresponde toujours ***
		System.out.println("******************************************************************");
		System.out.println("CreateAccountMB - createUser() | loginMB.getUser() : " + loginMB.getUser());
		System.out.println("******************************************************************");
	
		// *** transferer un USER avec un ROLE EN DUR !!! ***
		loginMB.getUserBu().update(loginMB.getUser());
		loginMB.submit();
		
		System.out.println("*** Retour dans ShowInfoMB - updateUser() ***");
		
		return "offerslist";
	}
	
	public String deleteUser() {
		
		System.out.println("*** Dans ShowInfoMB - updateUser() ***");
		
		loginMB.getUserBu().delete(loginMB.getUser());
		loginMB.deconnexion();
		
		System.out.println("*** Retour dans ShowInfoMB - updateUser() ***");
		
		return "home";
	}

	
}
